package com.dao.impl;

import com.dao.OrderDao;
import com.pojo.TbOrder;
import com.pojo.TbOrderList;
import com.utils.DBUtils;
import com.utils.SqlUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {

    private QueryRunner queryRunner=new QueryRunner(DBUtils.getSource());

    @Override
    public void addOrder(String oid, long uid, long money, String aid) {
        String sql = SqlUtils.getSql("order.add");
        try {
            queryRunner.insert(sql,new ScalarHandler<>(),oid,uid, money, aid);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public long addOrderDetail(String oid, long kzid, int num, long money) {
        String sql = SqlUtils.getSql("orderdetail.add");
        try {
            long detailld = queryRunner.insert(sql, new ScalarHandler<>(), oid, kzid, num, money);
            return detailld;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void updateStaus2Success(String id) {
        String sql = SqlUtils.getSql("order.paysuccess");
        try {
            queryRunner.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public String findStatusById(String id) {
        String sql = SqlUtils.getSql("order.status");
        try {
            String result = queryRunner.query(sql, new ScalarHandler<>(),id);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }



    public List<TbOrder>findAllOrder(long uid){
        String sql = SqlUtils.getSql("order.show");
        try {
            List<TbOrder>query=queryRunner.query(sql,new BeanListHandler<>(TbOrder.class),uid);
            return query;
        }catch (SQLException e){
            e.printStackTrace();
        }

            return new ArrayList<>();
    }

    @Override
    public void cancelOrder(String id) {
        String sql = "Update tb_order set status=5 where id=?";
        try {
            queryRunner.update(sql,id);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    }

